package com.itheima.redbaby.engine.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.bean.Category;
import com.itheima.redbaby.bean.Filter;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.dao.CategoryDao;
import com.itheima.redbaby.dao.domain.CategoryDo;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.engine.CategoryEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class CategoryEngineImpl extends BaseEngine implements CategoryEngine {
	private final String TAG = "CategoryEngineImpl";
	private CategoryDao categoryDao;
	
	@Override
	public List<Category> getCategoryList(int parentid) {
		//封装参数的map
		Map<String, Object> map = new HashMap<String, Object>();
		
		categoryDao = DaoAndEngineFactory.getImpl(CategoryDao.class);
		float localVersion = categoryDao.getCategoryVersion();
		
		map.put("version", localVersion);
		
		//获取到json
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_CATEGORY_URI, map);
		float retVersion = json.getFloat("version");
		List<Category> result = new ArrayList<Category>();
		
		if(localVersion < retVersion) {//如果服务器版本号高
			Log.i(TAG, "从服务器获取分类数据了" + json);
			
			List<Category> alllist = JSONObject.parseArray(json.getString("category"), Category.class);
			//保存所有最新分类信息到本地
			for (Category category : alllist) {
				CategoryDo domain = new CategoryDo();
				domain.setId(category.getId());
				domain.setIsleafnode(category.getIsleafnode());
				domain.setName(category.getName());
				domain.setParentid(category.getParentid());
				domain.setPic(category.getPic());
				domain.setTag(category.getTag());
				categoryDao.insert(domain);
				
				if(category.getParentid() == parentid) {
					result.add(category);
				}
			}
			
			//保存最新版本信息
			categoryDao.setCategoryVersion(retVersion);
		} else { //如果本地版本不比服务器低
			Log.i(TAG, "从本地获取分类数据了" + json);
			
			//从本地获取分类信息
			List<CategoryDo> locallist = categoryDao.findAll();
			for (CategoryDo domain : locallist) {
				if(domain.getParentid() == parentid) {
					Category category = new Category();
					category.setId(domain.getId());
					category.setIsleafnode(domain.getIsleafnode());
					category.setName(domain.getName());
					category.setParentid(domain.getParentid());
					category.setPic(domain.getPic());
					category.setTag(domain.getTag());
					result.add(category);
				}
			}
		}
		
		return result;
	}

	@Override
	public Map<String, List<?>> getCategoryProductList(int page, int pageNum, int cId, String orderby, String filter) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", page);
		map.put("pageNum", pageNum);
		map.put("cId", cId);
		map.put("orderby", orderby);
		//如果filter不为空，添加
		if(filter != null)
			map.put("filter", filter);
		//获取到json
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_PRODUCTLIST_URI, map);
		
		//获取商品列表
		List<Product> productlist = JSONObject.parseArray(json.getString("productlist"), Product.class);
		//获取筛选属性
		List<Filter> list_filter = JSONObject.parseArray(json.getString("list_filter"), Filter.class);

		Map<String,List<?>> retMap = new HashMap<String, List<?>>();
		retMap.put("productlist", productlist);
		retMap.put("filterlist", list_filter);
		return retMap;
	}
}
